package com.ian.controller;

import com.ian.util.BizUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.client.OAuth2AuthorizeRequest;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient;

/**
 * @author Witt
 * @version 1.0.0
 * @date 2022/5/17
 */
@RestController
public class TestController {

    @Autowired
    private ClientRegistrationRepository clientRegistrationRepository;

    @Autowired
    private OAuth2AuthorizedClientService authorizedClientService;

    @Autowired
    private OAuth2AuthorizedClientManager authorizedClientManager;

//    @Autowired
//    private WebClient webClient;

    @GetMapping("/getClientRegistration/{id}")
    public String getClientRegistration(@PathVariable String id) {
        ClientRegistration clientRegistration = clientRegistrationRepository.findByRegistrationId(id);
        System.out.println(clientRegistration);
        return clientRegistration.toString();
    }

    @GetMapping("/getClientToken/{id}")
    public String getClientAccessToken(@PathVariable String id, Authentication authentication) {
        OAuth2AuthorizedClient authorizedClient = authorizedClientService.loadAuthorizedClient(id, authentication.getName());
        return BizUtils.getClientTokenStr(id, authorizedClient);
    }

    /**
     * obtain the OAuth2 AccessToken
     *
     * @param authentication
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @GetMapping("/getOAuth2AccessToken")
    public String index(Authentication authentication,
                        HttpServletRequest servletRequest,
                        HttpServletResponse servletResponse) {

        OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
                .principal(authentication)
                .attributes(attrs -> {
                    attrs.put(HttpServletRequest.class.getName(), servletRequest);
                    attrs.put(HttpServletResponse.class.getName(), servletResponse);
                })
                .build();
        OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize(authorizeRequest);

        return BizUtils.getClientTokenStr("okta", authorizedClient);
    }

    /**
     * obtain the OAuth2 JWT AccessToken
     *
     * @param jwtAuthentication
     * @return
    @GetMapping("/getJwtAccessToken")
    public String getJwtAccessToken(JwtAuthenticationToken jwtAuthentication) {
        OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
                .principal(jwtAuthentication)
                .build();
        OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize(authorizeRequest);
        return BizUtils.getClientTokenStr("okta", authorizedClient);
    }
     */

    @GetMapping("/getClientTokenViaAnnotation")
    public String getClientTokenViaAnnotation(@RegisteredOAuth2AuthorizedClient("gitee") OAuth2AuthorizedClient authorizedClient) {
        return BizUtils.getClientTokenStr("gitee", authorizedClient);
    }

    /**
     * Providing the Authorized Client
     *
    @GetMapping("/getBodyViaWebClient")
    public String index(@RegisteredOAuth2AuthorizedClient("gitee") OAuth2AuthorizedClient authorizedClient) {
        String resourceUri = "https://www.baidu.com/";

        String body = webClient
                .get()
                .uri(resourceUri)
                .attributes(oauth2AuthorizedClient(authorizedClient))
                // set the ClientRegistration.getRegistrationId() as a request attribute
                .attributes(clientRegistrationId("gitee"))
                .retrieve()
                .bodyToMono(String.class)
                .block();
        System.out.println("body: " + body);
        return body;
    }
     */

}
